import java.util.Arrays;
import java.util.Comparator;



class Student1 implements Comparable<Student1> {
    public String name;
    public int age;

    public Student1(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student1{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student1 o) {
        return this.age - o.age;
    }
}

class Agecomparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}
class Namecomparator implements Comparator<Student1> {
    @Override
    public int compare(Student1 o1, Student1 o2) {
        return o1.name.compareTo(o2.name) ;
    }
}




public class Test2 {
    public static void main(String[] args) {
        Student student1 = new Student("zhangsan",12);
        Student student2 = new Student("lisi",15);

        Agecomparator agecomparator = new Agecomparator();
        Namecomparator namecomparator = new Namecomparator();

        System.out.println(agecomparator.compare(student1,student2));
        //System.out.println(namecomparator.compare(student1,student1));

        System.out.println("============");

        Student1[] students = new Student1[3];
        students[0] = new Student1("zhangsan", 20);
        students[1] = new Student1("lisi", 15);
        students[2] = new Student1("qiang", 18);

        Namecomparator namecomparator2 = new Namecomparator();
       // Agecomparator agecomparator2 = new Agecomparator();

        Arrays.sort(students,namecomparator2);
        Arrays.sort(students);
        System.out.println(Arrays.toString(students));

    }
    public static void mySort(Comparable[] comparables) {
        for (int i = 0; i < comparables.length-1; i++) {
            for (int j = 0; j < comparables.length-1-i; j++) {
                //交换
                Comparable tmp = comparables[j];
                comparables[j] = comparables [j+1];
                comparables[j+1] = tmp;
            }

        }
    }

    public static void main1(String[] args) {
        Student1[] students = new Student1[3];
        students[0] = new Student1("zhangsan",10);
        students[1] = new Student1("lisi",15);
        students[2] = new Student1("qiang",18);

        //Arrays.sort(students);
        mySort(students);
        System.out.println(Arrays.toString(students));

       // Comparable comparable =new Student1("lisi",15);
    }
}
